//
//  HCMineViewController.m
//  HappyChildren
//
//  Created by WillyZhao on 2016/12/16.
//  Copyright © 2016年 WillyZhao. All rights reserved.
//

#import "HCMineViewController.h"

#import "HCMineHeaderView.h"
#import "HCMineContentView.h"
#import "HCLogViewController.h"

#import "HCMineModel.h"
#import "HCLogViewController.h"

#import "XFWkwebView.h"
#import <AFNetworking.h>

@interface HCMineViewController () <HCMineHeaderViewDelegate,HCMineContentViewDelegate> {
    HCMineHeaderView *mine_headerView;
    HCMineContentView *mine_contentView;
    HCMineModel *model;
}

@end

@implementation HCMineViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
//    if ([PublicFunction、 isNull:HCGETUSER(@"userid")]) {
//        HCLogViewController *log = [[HCLogViewController alloc] init];
//        
//        UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
//        na.navigationBarHidden = YES;
//        [self.navigationController presentViewController:na animated:YES completion:nil];
//    }else{
        self.dataRequest.delegate = self;
        [self.dataRequest postHC_MineMessages:HCGETUSER(@"userid")];
//    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置title
    self.textTitle = @"我的";
    

    

    
    //设置背景颜色
    self.centerView.backgroundColor = YBKRGBCOLOR(241, 241, 241, 1);
    
    //设置右上角按钮
    [self setRightButtonImage:@"happy_mine_rightitemimage"];
    
    //初始化顶部
    [self initHeaderView1];
    
    //初始化内容
    [self initContentView];
    
}

#pragma mark 初始化头部视图
-(void)initHeaderView1 {
    mine_headerView = [[HCMineHeaderView alloc] initWithFrame:CGRectMake(0, 0, YHGetScreenWidth, YBKAdaptiveH(171))];
    mine_headerView.backgroundColor = [UIColor whiteColor];
    mine_headerView.delegate = self;
    [self.centerView addSubview:mine_headerView];
    
    mine_headerView.headerIMG.image = [UIImage imageNamed:@"happy_mine_headerIMG"];
    mine_headerView.nameLabel.text = @"WillyZhao";
}

#pragma mark 初始化具体内容视图
-(void)initContentView {
    mine_contentView = [[HCMineContentView alloc] initWithFrame:CGRectMake(0, YBKAdaptiveH(181), YHGetScreenWidth, YBKAdaptiveH((self.centerView.frame.size.height-YBKAdaptiveH(181))) )];
    mine_contentView.backgroundColor = [UIColor whiteColor];
    mine_contentView.delegate = self;
    [self.centerView addSubview:mine_contentView];
}

#pragma mark 顶部点击代理
-(void)hcMineHeaderViewUserInfoClicked:(HCMineHeaderView *)view {
    if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
                HCLogViewController *log = [[HCLogViewController alloc] init];
        
                UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
                na.navigationBarHidden = YES;
                [self.navigationController presentViewController:na animated:YES completion:nil];
    }else{
        [self pushChildrenViewController:@"HCMindHeadViewController" parameterObject:nil dataObject:nil];
    }
}

#pragma mark 余额，积分，优惠
-(void)hcMineHeaderViewClickedStyle:(HCMineHeaderViewClickStyle)style {
    switch (style) {
        case HCMineHeaderViewMoneyClicked:
            //余额
            if (!mine_headerView.imageView_VIP.hidden) {
                if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
                    HCLogViewController *log = [[HCLogViewController alloc] init];
                    
                    UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
                    na.navigationBarHidden = YES;
                    [self.navigationController presentViewController:na animated:YES completion:nil];
                }else{
                    [self pushChildrenViewController:@"HCVIPViewController" parameterObject:nil dataObject:nil];
                }
            }
            break;
        case HCMineHeaderViewScoreClicked:
            //积分
            NSLog(@"进入积分");
            break;
        case HCMineHeaderViewPreferentialClicked:
            //优惠
            NSLog(@"进入优惠");
            break;
        default:
            break;
    }
}

#pragma mark 内容部分点击代理
-(void)hcMineContentViewCellClickedIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
                HCLogViewController *log = [[HCLogViewController alloc] init];
                
                UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
                na.navigationBarHidden = YES;
                [self.navigationController presentViewController:na animated:YES completion:nil];
            }else{
                [self pushChildrenViewController:@"HCMineAppViewController" parameterObject:nil dataObject:nil];
            }
        }else if(indexPath.row == 1){
            
            if (!mine_headerView.imageView_VIP.hidden) {
                if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
                    HCLogViewController *log = [[HCLogViewController alloc] init];
                    
                    UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
                    na.navigationBarHidden = YES;
                    [self.navigationController presentViewController:na animated:YES completion:nil];
                }else{
                    [self pushChildrenViewController:@"HCGrowthAlbumViewController" parameterObject:nil dataObject:nil];
                }
            }else{
                if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
                    HCLogViewController *log = [[HCLogViewController alloc] init];
                    
                    UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
                    na.navigationBarHidden = YES;
                    [self.navigationController presentViewController:na animated:YES completion:nil];
                }else{
                    [self pushChildrenViewController:@"HCGrowthAlbumViewController" parameterObject:nil dataObject:nil];
                }
            }

        }else if (indexPath.row == 2){
            NSLog(@"我的兑换");
            [self pushChildrenViewController:@"HCMineExchangeViewController" parameterObject:nil dataObject:nil];
        }else if (indexPath.row == 3){
            //我的收藏
            [self pushChildrenViewController:@"HCMineCollectViewController" parameterObject:nil dataObject:nil];
        }
        else {
        
            if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
                
                [XHToast showBottomWithText:@"请登录"];
                return;
            }
            //地址管理
            [self pushChildrenViewController:@"HCAddressManagerController" parameterObject:nil dataObject:@"地址管理"];
        }
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
//            XFWkwebView *asd = [[XFWkwebView alloc] init];
//            AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//            
//            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//            
//            //拼接请求参数
//            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//            [dic setObject:[NSString stringWithFormat:@"%@",HCGETUSER(@"userid")] forKey:@"id"];
//            
//            __block __weak HCMineViewController *weakself = self;
//            
//            [manager POST:@"http://www.cdjiayibai.com/home/index/buy_goods" parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
//                
//            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
//                NSLog(@"商品详情数据返回:%@",json);
//                if (![PublicFunction isNull:json[@"url"]]) {
//                    [asd loadWebURLSring:[NSString stringWithFormat:@"%@",json[@"url"]]];
//                    [weakself.navigationController pushViewController:asd animated:YES];
//                }
//            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//                NSLog(@"请求失败");
//            }];
            
            [self pushChildrenViewController:@"HCGoodsScoreViewController" parameterObject:nil dataObject:nil];
            
        }else if (indexPath.row == 1) {
            //积分商城
        }else{
            
        }
    }else{
        if (indexPath.row == 0) {
            [self pushChildrenViewController:@"HCCtnterViewController" parameterObject:nil dataObject:nil];
        }else if (indexPath.row == 1) {
            
            NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id1240768515"];
            
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }
    }

}

#pragma mark 网络请求返回
-(void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {
    NSLog(@"个人信息界面返回数据:%@",dictionary);
    model = [[HCMineModel alloc] initWithDictionary:dictionary error:nil];
    
    mine_headerView.nameLabel.text = [NSString stringWithFormat:@"%@",model.index.name];
    

    if ([PublicFunction isNull:model.index.path]) {
    }else{
        [mine_headerView.headerIMG sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.index.path]]];
    }
    
    if ([[NSString stringWithFormat:@"%@",model.index.vipcard] length]>1) {
        mine_headerView.imageView_VIP.hidden = NO;
        mine_headerView.balanceLabel.text = [NSString stringWithFormat:@"%@",model.index.vipcard];
    }else{
        if (![PublicFunction isNull:model.index.vipcard]) {
            mine_headerView.imageView_VIP.hidden = NO;
            mine_headerView.balanceLabel.text = [NSString stringWithFormat:@"%@",model.index.vipcard];
        }else{
            mine_headerView.imageView_VIP.hidden = YES;
            mine_headerView.balanceLabel.text = [NSString stringWithFormat:@"非会员"];
        }
    }
    

    
    if ([PublicFunction isNull:model.index.point]) {
        mine_headerView.scoreLabel.text = @"0";
    }else{
        mine_headerView.scoreLabel.text = [NSString stringWithFormat:@"%@",model.index.point];
    }
    
    mine_headerView.preferentialLabel.text = [NSString stringWithFormat:@"%@",model.index.favourable];
    
}

-(void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {
    NSLog(@"个人信息网络请求失败");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
